const { sqlConnect } = require('../config/db')
// 查询登录
const queryLogin = (params) => {
  return new Promise((resolve, reject) => {
    /**
     * sql, cb
     */
    sqlConnect(
      'select * from account_admin where account = ? and password = ?',
      params,
      (err, data) => {
        if(err) reject(err)
        resolve(data)
      },
    )
  })
}
// 查询个人信息
const queryUserInfo = (params) => {
  return new Promise((resolve, reject) => {
    /**
     * sql, cb
     */
    sqlConnect(
      'select * from account_admin where account = ?',
      params,
      (err, data) => {
        if(err) reject(err)
        resolve(data)
      },
    )
  })
}
// 新增品牌
const brandAddSql = (params) => {
  return new Promise((resolve, reject) => {
    sqlConnect(
      'insert into brand(name,logo,user_id,package_id,status) values (?,?,?,?,?)',
      params,
      (err, data) => {
        if (err) {
          reject(err)
        } else {
          resolve(data)
        }
      },
    )
  })
}
// 删除品牌
const brandDelSql = (params) => {
  return new Promise((resolve, reject) => {
    sqlConnect('delete from brand where id = ?', params, (err, data) => {
      if (err) {
        reject(err)
      } else {
        resolve(data)
      }
    })
  })
}
// 品牌列表分页
const brandListSql = (params) => {
  return new Promise((resolve, reject) => {
    sqlConnect('select * from brand limit ?,?', params, (err, data) => {
      if (err) {
        reject(err)
      } else {
        resolve(data)
      }
    })
  })
}
// 品牌列表总条数
const brandCountSql = (params) => {
  return new Promise((resolve, reject) => {
    sqlConnect('select count(*) from brand', params, (err, data) => {
      if (err) {
        reject(err)
      } else {
        resolve(data)
      }
    })
  })
}
/**
 * 用户组
 */
// 新增用户组
const userGroupAddSql = (params) => {
  return new Promise((resolve, reject) => {
    sqlConnect(
      'insert into platform_user_group(sort, name, service_id, status) values (?,?,?,?)',
      params,
      (err, data) => {
        if (err) {
          reject(err)
        } else {
          resolve(data)
        }
      },
    )
  })
}
// 修改用户组
const userGroupUpdateSql = (params) => {
  return new Promise((resolve, reject) => {
    sqlConnect(
      'update platform_user_group set sort=?, name=?, service_id=?, status=? where id=?',
      params,
      (err, data) => {
        if (err) {
          reject(err)
        } else {
          resolve(data)
        }
      },
    )
  })
}
// 查询用户组（分页）
const userGroupListSql = (params) => {
  return new Promise((resolve, reject) => {
    sqlConnect(
      'select * from platform_user_group limit ?,?',
      params,
      (err, data) => {
        if (err) {
          reject(err)
        } else {
          resolve(data)
        }
      },
    )
  })
}
// 查询用户组总条数
const userGroupCountSql = (params) => {
  return new Promise((resolve, reject) => {
    sqlConnect(
      'select count(*) from platform_user_group',
      params,
      (err, data) => {
        if (err) {
          reject(err)
        } else {
          resolve(data)
        }
      },
    )
  })
}
// 删除用户组(ids批量)
const userGroupDelSql = (params) => {
  let queryStr = 'id = ?'
  if(Array.isArray(params)){
    queryStr = `${params.map((i)=>'id = ?').join(' or ')}`
  }
  return new Promise((resolve, reject) => {
    sqlConnect(
      'delete from platform_user_group where '+queryStr,
      params,
      (err, data) => {
        if (err) {
          reject(err)
        } else {
          resolve(data)
        }
      },
    )
  })
}
// 查询全部用户组
const userGroupAllSql = (params) => {
  return new Promise((resolve, reject) => {
    sqlConnect(
      'select count(*) from platform_user_group',
      params,
      (err, data) => {
        if (err) {
          reject(err)
        } else {
          resolve(data)
        }
      },
    )
  })
}
/**
 * 用户客服
 */
// 新增客服
const serviceAddSql = (params) => {
  return new Promise((resolve, reject) => {
    sqlConnect(
      'insert into platform_service(sort, name, phone, wx_code, status) values (?,?,?,?,?)',
      params,
      (err, data) => {
        if (err) {
          reject(err)
        } else {
          resolve(data)
        }
      },
    )
  })
}
// 修改用户客服
const serviceUpdateSql = (params) => {
  return new Promise((resolve, reject) => {
    sqlConnect(
      'update platform_service set sort=?, name=?, phone=?, wx_code=?, status=? where id=?',
      params,
      (err, data) => {
        if (err) {
          reject(err)
        } else {
          resolve(data)
        }
      },
    )
  })
}
// 查询用户客服（分页）
const serviceListSql = (params) => {
  console.log('params',params)
  return new Promise((resolve, reject) => {
    sqlConnect(
      'select * from platform_service order by sort desc limit ?,?',
      params,
      (err, data) => {
        if (err) {
          reject(err)
        } else {
          resolve(data)
        }
      },
    )
  })
}
// 查询用户客服总条数
const serviceCountSql = (params) => {
  return new Promise((resolve, reject) => {
    sqlConnect(
      'select count(*) from platform_service',
      params,
      (err, data) => {
        if (err) {
          reject(err)
        } else {
          resolve(data)
        }
      },
    )
  })
}
// 删除用户客服(ids批量)
const serviceDelSql = (params) => {
  return new Promise((resolve, reject) => {
    let queryStr = 'id = ?'
    if(Array.isArray(params)){
      queryStr = `${params.map((i)=>'id = ?').join(' or ')}`
    }
    sqlConnect(
      'delete from platform_service where '+queryStr,
      params,
      (err, data) => {
        if (err) {
          reject(err)
        } else {
          resolve(data)
        }
      },
    )
  })
}
// 获取全部客服
const serviceAllSql = (params) => {
  return new Promise((resolve, reject) => {
    sqlConnect(
      'select * from platform_service',
      params,
      (err, data) => {
        console.log(err, data)
        if (err) {
          reject(err)
        } else {
          resolve(data)
        }
      },
    )
  })
}
/**
 * 用户管理
 */
// 新增用户
const userAddSql = (params) => {
  return new Promise((resolve, reject) => {
    sqlConnect(
      'insert into platform_user(phone, name, password, group_id, store_num, status) values (?,?,?,?,?)',
      params,
      (err, data) => {
        if (err) {
          reject(err)
        } else {
          resolve(data)
        }
      },
    )
  })
}
// 修改用户
const userUpdateSql = (params) => {
  return new Promise((resolve, reject) => {
    sqlConnect(
      'update platform_user set phone=?, name=?, group_id=?, store_num=?, status=? where id=?',
      params,
      (err, data) => {
        if (err) {
          reject(err)
        } else {
          resolve(data)
        }
      },
    )
  })
}
// 查询用户（分页）
const userListSql = (params) => {
  return new Promise((resolve, reject) => {
    sqlConnect(
      'select * from platform_user limit ?,?',
      params,
      (err, data) => {
        if (err) {
          reject(err)
        } else {
          resolve(data)
        }
      },
    )
  })
}
// 查询用户总条数
const userCountSql = (params) => {
  return new Promise((resolve, reject) => {
    sqlConnect(
      'select count(*) from platform_user',
      params,
      (err, data) => {
        if (err) {
          reject(err)
        } else {
          resolve(data)
        }
      },
    )
  })
}
// 删除用户(id)
const userDelSql = (params) => {
  return new Promise((resolve, reject) => {
    sqlConnect(
      'delete from platform_user where id = ?',
      params,
      (err, data) => {
        if (err) {
          reject(err)
        } else {
          resolve(data)
        }
      },
    )
  })
}
module.exports = {
  queryLogin,
  queryUserInfo,
  brandAddSql,
  brandDelSql,
  brandListSql,
  brandCountSql,
  userGroupAddSql,
  userGroupUpdateSql,
  userGroupListSql,
  userGroupCountSql,
  userGroupDelSql,
  userGroupAllSql,
  serviceAddSql,
  serviceUpdateSql,
  serviceListSql,
  serviceCountSql,
  serviceDelSql,
  serviceAllSql,
  userAddSql,
  userUpdateSql,
  userListSql,
  userCountSql,
  userDelSql,
}
